<?php
namespace app\common\model;

class Deal extends BaseModel
{
    public function getNormalDeals($data = [])
    {
        $data['status'] = 1;
        $order = ['id' => 'desc'];
        $result = $this->where($data)
            ->order($order)
            ->paginate();
        // echo $this->getLastSql();

        return $result;
    }

    /**
     * 根据分类 城市获取商品数据
     * @param $id 分类ID
     * @param $cityId 城市ID
     * @param int $limit 获取条数
     */
    public function getNormalDealByCategoeyCityId($id, $cityId, $limit = 10)
    {
        $data = [
            //'end_time' => ['gt', time()],
            'category_id' => $id,
            'city_id' => $cityId,
            'status' => 1,
        ];
        $order = [
            'listorder' => 'desc',
            'id' => 'desc'
        ];
        $result = $this->where($data)
            ->order($order);
        if ($limit) {
            $result = $result->limit($limit);
        }
        return $result->select();
    }

    public function getDealByConditions($data = [], $orders)
    {
        if (!empty($orders['order_sales'])) {
            $order['buy_count'] = 'desc';
        }
        if (!empty($orders['order_price'])) {
            $order['current_price'] = 'desc';
        }
        if (!empty($orders['order_time'])) {
            $order['create_time'] = 'desc';
        }
        $orders['id'] = 'desc';
        $datas[] = "end_time > " . time();
        if (!empty($data['se_category_id'])) {
            $datas[] = "find_in_set(" . $data['se_category_id'] . ",se_category_id)";
        }
        if (!empty($data['category_id'])) {
            $datas[] = 'category_id=' . $data['category_id'];
        }
        if (!empty($data['city_id'])) {
            $datas[] = 'city_id=' . $data['city_id'];
        }
        $datas[] = 'status=1';
        $result = $this->where(implode(' AND ', $datas))
            ->order($orders)
            ->paginate();
        return $result;
    }
}